package com.nativetalk.service.found.search;

import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.FoundChange.TdNtMembFoundChangeList;
import com.nativetalk.bean.FoundChange.TdNtMembFoundChangeUpdate;
import com.nativetalk.common.ValidationMessages;
import com.nativetalk.dao.TdNtMembFoundChangeDao;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lizhun on 15/12/28.
 * Modified by wangzheng on 17//3/8
 */
@Service
public class FoundChangeSearchServiceImpl implements  FoundChangeSearchService {
    private static Logger logger = Logger.getLogger(FoundChangeSearchServiceImpl.class);
    @Resource
    private TdNtMembFoundChangeDao tdNtMembFoundChangeDao;
    @Override
    public RetInfo findFoundChangeByMembId(Integer memb_id, int currentPage, int pageSize) {
        String logInfo = this.getClass().getName() + ":findFoundChangeByMembId:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            int totalPage;//总页数
            int totalRecord = tdNtMembFoundChangeDao.findTdNtMembFoundChangeSize(memb_id);//总条数
            if (totalRecord % pageSize == 0) {
                totalPage = totalRecord/pageSize;
            } else {
                totalPage = (totalRecord/pageSize) + 1;
            }
            if (currentPage >= totalPage) {
                currentPage = totalPage;
            }
            if (currentPage <= 1) {
                currentPage = 1;
            }
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("memb_id",memb_id);
            map.put("currentPage",(currentPage-1)*10);
            map.put("pageSize",pageSize);

            List<TdNtMembFoundChangeList> foundChangeLists = tdNtMembFoundChangeDao.findTdNtMembFoundChangePage(map);
            if (foundChangeLists == null || foundChangeLists.size() <= 0) {
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip(ValidationMessages.getValue("MEMB.FOUN.RESULT.NULL"));
            } else {
                for(TdNtMembFoundChangeList foundChangeList : foundChangeLists){
                    if(null != foundChangeList.getType() && 4 == foundChangeList.getType()){
                        foundChangeList.setMfchg_change_name("其它");
                    }
                }
            }

            Map<String, Object> result = new HashMap<>();
            result.put("foundChangeLists", foundChangeLists);
            result.put("totalPage", totalPage);
            result.put("totalRecord", totalRecord);
            result.put("currentPage", currentPage);
            retInfo.setObj(result);
            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("获取资金变动列表成功");

        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    @Override
    public RetInfo findFoundChangeByNumber(String mfchg_number) {
        String logInfo = this.getClass().getName() + ":findFoundChangeByNumber:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            TdNtMembFoundChangeUpdate tdNtMembFoundChangeUpdate = tdNtMembFoundChangeDao.findTdNtMembFoundChangeBack(mfchg_number);
            if (tdNtMembFoundChangeUpdate != null && tdNtMembFoundChangeUpdate.getState().equals("2")) {
                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip("支付成功");
            } else {
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip("支付失败");
            }

        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
}
